package com.digitalchina.lxnsy.activity.old;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAgr;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.AgrListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.AgrListHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2019/4/17 10:20
 * 修改备注：
 * ================================================
 */
public class SearchNewAgrActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.iv_cancel)
    ImageView ivCancel;
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

//    private String farmOperationId = ""; // 农事操作id
    private String productStandardId = ""; // 生产标准id
    private String ID = "";
    private String searchContent = "";

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;

    private List<AgrListVO> voList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_search_new_agr);
        setTitle("搜索新农艺");
    }

    @Override
    public void initMember() {

        productStandardId = (String) getIntent().getSerializableExtra("ID");
//        String array[] = ID.split("&");
//
//        farmOperationId = array[0];
//        productStandardId = array[1];

        recyclerView.setLayoutManager(new LinearLayoutManager(SearchNewAgrActivity.this));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(SearchNewAgrActivity.this, 1, 0, 0));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(SearchNewAgrActivity.this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new AgrListHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                AgrListVO vo = (AgrListVO) adapter.getAllData().get(position);
                GotoUtil.gotoActivity(SearchNewAgrActivity.this, NewAgrH5Activity.class, "AgrListVO", vo);
            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 隐藏软键盘
                    InputMethodManager imm = (InputMethodManager) textView.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(textView.getApplicationWindowToken(), 0);
                    }
                    // 刷新列表数据
                    if (!searchContent.equals(etSearch.getText().toString())) {
                        searchContent = etSearch.getText().toString();
                        onRefresh();
                    }
                    return true;
                }
                return false;
            }
        });

        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchContent = "";
                etSearch.setText("");
                onRefresh();
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG.MSG_SEARCH_NEW_AGR_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_SEARCH_NEW_AGR_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), "newAgricultureList", AgrListVO.class);


        if (voList == null)
            return;

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);


        if (voList != null) {
            if (voList.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessAgr.searchNewAgrV2(SearchNewAgrActivity.this, searchContent,  productStandardId, page, limit, mHandler);
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            BusinessAgr.searchNewAgrV2(SearchNewAgrActivity.this, searchContent, productStandardId, page, limit, mHandler);
        }
    }
}
